package com.zhonghan.recharge.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhonghan.common.bean.Result;
import com.zhonghan.common.utils.StringUtils;
import com.zhonghan.recharge.service.UserService;

@Controller
public class UserController {
	
	@Autowired
	private UserService userService;
	
	
	@RequestMapping(value="/admin/addUser",method=RequestMethod.GET)
	public String showAddUser(){
		
		
		return "admin/addUser";
	}
	
	@RequestMapping(value="/admin/addUser",method=RequestMethod.POST)
	@ResponseBody
	public Result addUser(String username,String password,String telephone,String nickname,String realname,Integer roleId){
		
		Result result = userService.addUser(username,password,telephone,nickname,realname,roleId);
		
		return result;
	}
	
	@RequestMapping(value="/admin/deleteUser",method=RequestMethod.POST)
	@ResponseBody
	public Result deleteUser(String id,String ids){
		
		Result result = new Result();
		
		try {
			if(StringUtils.isNotEmpty(id)){
				int code = userService.deleteUserById(Integer.parseInt(id));
				if(code > 0){
					result.setSuccess("1", null, "删除成功!");
				}else{
					result.setFailure("-1", "删除失败!");
				}
				return result;
			}
			
			if(StringUtils.isNotEmpty(ids)){
				String[] idsArray = ids.split(",");
				
				for (String idstr : idsArray) {
					userService.deleteUserById(Integer.parseInt(idstr));
				}
				
			}
			
		} catch (Exception e) {
			result.setFailure("-1", "系统繁忙,请稍后再试!");
			e.printStackTrace();
		}
		
		return result;
	}
	
	
	
}
